package com.sservice.common;

import com.sservice.urp.vo.UserVo;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by byte2 on 2017-05-31.
 * 管理后台登录权限判断
 */
public class AuthAdmin extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            String url = request.getRequestURI();
            if (url.indexOf("/admin") > -1 && (!(url.equals("/admin/user/nologin") || url.equals("/admin/user/login") || url.equals("/admin/code/captcha-image")))) { //后台请求且非登录请求
                UserVo user = (UserVo) request.getSession().getAttribute(Constants.USER);
                if (user == null) {
                    Map map = new HashMap<String, Object>();
                    response.sendRedirect("/api/admin/user/nologin");
//                    request.getRequestDispatcher().forward(request,response);
                    return false;
                }
            }
            return true;
        } else {
            return super.preHandle(request, response, handler);
        }
    }
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
//            throws Exception {
//
//    }
}
